
package protocol.transactions;

import java.util.LinkedList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import protocol.XoneProtocol;
import protocol.XoneServiceCode;
import protocol.meta.RecommAppInfo;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import app.BaseApplication;

import com.netease.http.THttpRequest;
import com.netease.task.example.StringAsyncTransaction;

public class GetRecommAppTransaction extends StringAsyncTransaction {

    /**
     * 推荐APP,缓存记录的配置文件
     */
    public static final String RECOMM_APP_JSON = "recomm_app_json";

    public GetRecommAppTransaction() {
        super(XoneBaseTransaction.TRANSACTION_TYPE_GET_RECOMM_APP);
    }

    @Override
    protected void onTransactionError(int errCode, Object obj) {
        String json = PreferenceManager.getDefaultSharedPreferences(
                BaseApplication.getAppInstance()).getString(RECOMM_APP_JSON, "");
        if (json != null) {
            // 解析后返回
            JSONArray jsonarr;
            try {
                jsonarr = new JSONArray(json);
                List<RecommAppInfo> list = new LinkedList<RecommAppInfo>();
                if (jsonarr != null) {
                    int length = jsonarr.length();
                    for (int index = 0; index < length; index++) {
                        RecommAppInfo info = new RecommAppInfo(jsonarr.getString(index));
                        list.add(info);
                    }
                }
                notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, list);
            } catch (JSONException e) {
                notifyError(XoneServiceCode.ERR_CODE_DATA_PARSE_EXCEPTION, obj);
                e.printStackTrace();
            }
        } else {
            notifyError(errCode, obj);
        }
    }

    @Override
    protected void onTransactionSuccess(int code, Object obj) {
        if (obj instanceof String) {
            String response = (String)obj;
            if (!TextUtils.isEmpty(response)) {
                // 写入本地
                PreferenceManager.getDefaultSharedPreferences(BaseApplication.getAppInstance())
                        .edit().putString(RECOMM_APP_JSON, response).commit();
                try {
                    JSONArray jsonarr = new JSONArray(response);
                    List<RecommAppInfo> list = new LinkedList<RecommAppInfo>();
                    if (jsonarr != null) {
                        int length = jsonarr.length();
                        for (int index = 0; index < length; index++) {
                            RecommAppInfo info = new RecommAppInfo(jsonarr.getString(index));
                            list.add(info);
                        }
                    }
                    notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, list);
                    return;
                } catch (JSONException e1) {
                    onTransactionError(XoneServiceCode.ERR_CODE_DATA_PARSE_EXCEPTION, null);
                    e1.printStackTrace();
                }
            }
        }
        onTransactionError(XoneServiceCode.TRANSACTION_FAIL, null);
    }

    @Override
    public void onTransact() {
        THttpRequest request = XoneProtocol.getInstance().createGetRecommAppRequest();
        sendRequest(request);
    }

}
